package com.sgcc.fj.pmos.e_fj_home.presenter;

import com.blankj.utilcode.util.SPUtils;
import com.sgcc.fj.pmos.common.base.BasePresenter;
import com.sgcc.fj.pmos.common.bean.RatioCurveBean;
import com.sgcc.fj.pmos.common.http.HttpInterConst;
import com.sgcc.fj.pmos.common.http.HttpRequestUtils;
import com.sgcc.fj.pmos.common.http.MvcNetCallback;
import com.sgcc.fj.pmos.common.http.MvpNetCallback;
import com.sgcc.fj.pmos.common.utils.ListPageUtil;
import com.sgcc.fj.pmos.e_fj_home.bean.ContracttExecutionBean;
import com.sgcc.fj.pmos.e_fj_home.bean.DynamicNetBean;
import com.sgcc.fj.pmos.e_fj_home.bean.HomeGoodsBean;
import com.sgcc.fj.pmos.e_fj_home.bean.StoreBean;
import com.sgcc.fj.pmos.e_fj_home.contract.HomeContract;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class HomePresenter extends BasePresenter<HomeContract.View> implements HomeContract.Presenter {

    @Override
    public void getContractExecution(boolean isloading) {
        Map<String, Object> params = new HashMap<>();
        HttpRequestUtils.getInstance().request(params, HttpInterConst.HOME_CONTRACT_EXECUTION, new MvpNetCallback<ContracttExecutionBean>(getView(), isloading) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁
                if (!isViewAttached()) {
                    return;
                }
                getView().queryContractFailed();
            }

            @Override
            public void onResponse(ContracttExecutionBean response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                getView().queryContractSuccess(response, isloading);
            }
        });
    }

    @Override
    public void getStoreList(boolean isloading) {
        Map<String, Object> params = new HashMap<>();
        params.put("owernType", "");
        params.put("creditRating", "");
        params.put("evaScope", "");
        params.put("geogrregionId", "");
        params.put("regcapital", "");
        params.put("saleEnergyYear", "");
        params.put("saleMemberNum", "");
        params.put("pageNum", 1);
        params.put("pageSize", 4);
        params.put("membersSdName", "");
        params.put("saleMemberNumOrder", "");
        params.put("saleEnergyYearOrder", "");
        params.put("comprehensiveSort", "");
        params.put("membersSdId", "BE738C62781AD91447C8516F11B5EC2E");
        params.put("_t", System.currentTimeMillis());
        HttpRequestUtils.getInstance().request(params, HttpInterConst.HOME_STORE, new MvpNetCallback<StoreBean>(getView(), isloading) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁
                if (!isViewAttached()) {
                    return;
                }
                getView().queryStoreListFailed();
            }

            @Override
            public void onResponse(StoreBean response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                getView().queryStoreListSuccess(response, isloading);
            }
        });
    }


    @Override
    public void getRatioCurve(boolean isloading) {
        HttpRequestUtils.getInstance().request(new HashMap<>(), HttpInterConst.GET_RATIO_CURVE, new MvcNetCallback<RatioCurveBean>(true) {

            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                String ratioId = "1";
                getGoodsList(ratioId, isloading);
            }

            @Override
            public void onResponse(RatioCurveBean response, int id) {
                super.onResponse(response, id);
                String sp_ratioid = SPUtils.getInstance().getString("ratioId", "-1");
                if ("-1".equals(sp_ratioid)) {
                    String ratioId = "1";
                    if (null != response) {
                        if ("200".equals(response.getCode())) {
                            if (response.getData() != null) {
                                if (response.getData().size() > 0) {
                                    ratioId = response.getData().get(0).getRatioId();
                                    SPUtils.getInstance().put("ratioId", ratioId);
                                    SPUtils.getInstance().put("ratioName", response.getData().get(0).getRatioName());
                                }
                            }
                        }
                    }
                    getGoodsList(ratioId, isloading);
                } else {
                    getGoodsList(sp_ratioid, isloading);
                }
            }
        });
    }


    public void getGoodsList(String ratioId, boolean isloading) {
        Map<String, Object> params = new HashMap<>();
        params.put("pageNum", 1);
        params.put("pageSize", 5);
        params.put("combName", "");
        params.put("ratioId", ratioId);
        params.put("membersSdId", "");
        params.put("sort", "");
        params.put("soldEnergySort", "");
        params.put("avgPriceSort", "");
        params.put("createDateSort", "");
        Map<String, Object> params1 = new HashMap<>();
        params.put("packageInfoCombVo", params1);
        params.put("_t", System.currentTimeMillis());
        HttpRequestUtils.getInstance().request(params, HttpInterConst.HOME_PACKAGE, new MvpNetCallback<HomeGoodsBean>(getView(), isloading) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁
                if (!isViewAttached()) {
                    return;
                }
                getView().queryGoodsFailed();
            }

            @Override
            public void onResponse(HomeGoodsBean response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                getView().queryGoodsSuccess(response, isloading);
            }
        });
    }


    public void getMarketListData() {
        Map<String, Object> params = new HashMap<>();
        params.putAll(ListPageUtil.getPageInfo(0));
        params.put("_t", System.currentTimeMillis());
//        data.put("mktYear", 2022);

        HttpRequestUtils.getInstance().request(params, HttpInterConst.HOME_MARKETDAYNAMICS, new MvpNetCallback<DynamicNetBean>(getView(), true) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                // 失败回调 判断界面是否销毁
                if (!isViewAttached()) {
                    return;
                }
//                getView().queryFaile();
            }

            @Override
            public void onResponse(DynamicNetBean response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                getView().queryNetDataSuccess(response);
            }
        });
    }
}
